package com.trs.util;

public class HttpClientConfig {
	/**
	 * 连接池最大的连接数
	 */
	private int maxConnect;

	/**
	 * 连接每个域名的最大连接数
	 */
	private int maxPerRoute;

	/**
	 * 获取从连接池获取连接的最长时间，单位是毫秒；
	 */
	private int connectionRequestTimeout;

	/**
	 * 获取创建连接的最长时间，单位是毫秒；
	 */
	private int connectionTimeout;

	/**
	 * 获取数据传输的最长时间，单位是毫秒
	 */
	private int socketTimeout;

	/**
	 * 用空闲连接过期时间,重用空闲连接时会先检查是否空闲时间超过这个时间，如果超过，释放socket重新建
	 */
	private int validateAfterInactivity;

	/**
	 * 是否保持长连接
	 */
	private boolean keepAlive;

	public int getValidateAfterInactivity() {
		return validateAfterInactivity;
	}

	public void setValidateAfterInactivity(int validateAfterInactivity) {
		this.validateAfterInactivity = validateAfterInactivity;
	}

	private HttpClientConfig(Builder builder) {
		setMaxConnect(builder.maxConnect);
		setMaxPerRoute(builder.maxPerRoute);
		setConnectionRequestTimeout(builder.connectionRequestTimeout);
		setConnectionTimeout(builder.connectionTimeout);
		setSocketTimeout(builder.socketTimeout);
		setValidateAfterInactivity(builder.validateAfterInactivity);
		setKeepAlive(builder.keepAlive);
	}

	public static Builder newBuilder() {
		return new Builder();
	}

	public int getMaxConnect() {
		return maxConnect;
	}

	public void setMaxConnect(int maxConnect) {
		this.maxConnect = maxConnect;
	}

	public int getMaxPerRoute() {
		return maxPerRoute;
	}

	public void setMaxPerRoute(int maxPerRoute) {
		this.maxPerRoute = maxPerRoute;
	}

	public int getConnectionRequestTimeout() {
		return connectionRequestTimeout;
	}

	public void setConnectionRequestTimeout(int connectionRequestTimeout) {
		this.connectionRequestTimeout = connectionRequestTimeout;
	}

	public int getConnectionTimeout() {
		return connectionTimeout;
	}

	public void setConnectionTimeout(int connectionTimeout) {
		this.connectionTimeout = connectionTimeout;
	}

	public int getSocketTimeout() {
		return socketTimeout;
	}

	public void setSocketTimeout(int socketTimeout) {
		this.socketTimeout = socketTimeout;
	}

	public boolean isKeepAlive() {
		return keepAlive;
	}

	public void setKeepAlive(boolean keepAlive) {
		this.keepAlive = keepAlive;
	}

	public static final class Builder {
		private int maxConnect;
		private int maxPerRoute;
		private int connectionRequestTimeout;
		private int connectionTimeout;
		private int socketTimeout;
		private int validateAfterInactivity;
		private boolean keepAlive;

		private Builder() {
		}

		/**
		 * 连接池最大的连接数
		 */
		public Builder maxConnect(int maxConnect) {
			this.maxConnect = maxConnect;
			return this;
		}

		/**
		 * 连接每个域名的最大连接数
		 */
		public Builder maxPerRoute(int maxPerRoute) {
			this.maxPerRoute = maxPerRoute;
			return this;
		}

		/**
		 * 获取从连接池获取连接的最长时间，单位是毫秒；
		 */
		public Builder connectionRequestTimeout(int connectionRequestTimeout) {
			this.connectionRequestTimeout = connectionRequestTimeout;
			return this;
		}

		/**
		 * 获取创建连接的最长时间，单位是毫秒；
		 */
		public Builder connectionTimeout(int connectionTimeout) {
			this.connectionTimeout = connectionTimeout;
			return this;
		}

		/**
		 * 获取数据传输的最长时间，单位是毫秒
		 */
		public Builder socketTimeout(int socketTimeout) {
			this.socketTimeout = socketTimeout;
			return this;
		}

		/**
		 * 用空闲连接过期时间,重用空闲连接时会先检查是否空闲时间超过这个时间，如果超过，释放socket重新建
		 */
		public Builder validateAfterInactivity(int validateAfterInactivity) {
			this.validateAfterInactivity = validateAfterInactivity;
			return this;
		}

		/**
		 * 是否保持长连接
		 */
		public Builder keepAlive(boolean keepAlive) {
			this.keepAlive = keepAlive;
			return this;
		}

		public HttpClientConfig build() {
			return new HttpClientConfig(this);
		}
	}
}
